//
//  UseTimeDataViewController.swift
//  EasePods
//
//  Created by 李然 on 2024/5/30.
//

import UIKit
import EarbudsCommon
import JXSegmentedView

class UseTimeDetailViewController: ViewController {

    let viewModel: UseTimeDetailViewModel
    
    let timeRangeType: TimeRangeType
    
    init(timeRangeType: TimeRangeType, date: Date) {
        self.timeRangeType = timeRangeType
        self.viewModel = UseTimeDetailViewModel(date: date)
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func makeUI() {
        super.makeUI()
        
        view.backgroundColor = R.color.common_10_bg_page()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        super.bindViewModel()
        
        let input = UseTimeDetailViewModel.Input(dataRange: Observable.just(self.timeRangeType))
        let output = self.viewModel.transform(input: input)
        
        output.dataSource.bind(to: tableView.rx.items) { tableView, row, item in
            switch item {
            case .dayChart(let config):
                let cell = tableView.dequeueReusableCell(withIdentifier: UseTimeChartDataCell.reuseIdentifier) as! UseTimeChartDataCell
                cell.setData(config)
                return cell
            case .actual(let actualTimeMin):
                let cell = tableView.dequeueReusableCell(withIdentifier: UseTimeRealTimeCell.reuseIdentifier) as! UseTimeRealTimeCell
                cell.setData(actualTimeMin)
                return cell
            case .analize(let data):
                let cell = tableView.dequeueReusableCell(withIdentifier: UseTimeDetailInfoCell.reuseIdentifier) as! UseTimeDetailInfoCell
                cell.setData(data)
                return cell
            }
        }.disposed(by: rx.disposeBag)
    }
    
    lazy var tableView: TableView = {
        let view = TableView()
        view.showsVerticalScrollIndicator = false
        view.separatorStyle = .none
        view.register(UseTimeChartDataCell.self, forCellReuseIdentifier: UseTimeChartDataCell.reuseIdentifier)
        view.register(UseTimeRealTimeCell.self, forCellReuseIdentifier: UseTimeRealTimeCell.reuseIdentifier)
        view.register(UseTimeDetailInfoCell.self, forCellReuseIdentifier: UseTimeDetailInfoCell.reuseIdentifier)
        return view
    }()
}

extension UseTimeDetailViewController: JXSegmentedListContainerViewListDelegate {
    
    func listView() -> UIView {
        return self.view
    }
}
